package model

import (
	"gorm.io/gorm"
	"monment/handler/response"
)

type Post struct {
	gorm.Model
	UserId    int    `gorm:"type:int" json:"userId"`
	Content   string `gorm:"type:varchar(100)" json:"content"`
	PostImage string `gorm:"type:varchar(255)" json:"postImage"`
	// VisRange 默认公开可见,私密和部分可见其他设置
	VisRange string `gorm:"type:varchar(20);default:public;index" json:"visRange"`
}

func (p *Post) Publish(db *gorm.DB) error {
	return db.Create(&p).Error
}

func (p *Post) GetPostList(db *gorm.DB, page int, size int) (list []*response.PostList, err error) {
	offset := (page - 1) * size
	err = db.Model(&p).Select("posts.user_id,posts.content,posts.post_image").
		Offset(offset).Limit(size).
		Order("created_at desc").Find(&list).Error
	return
}
